#!/usr/env/bin python
# -*- coding: utf-8 -*-

# @Time    : 2020-08-31 9:41 上午
# @Author  : yangdy
# @File    : 2020-08-31.841.钥匙和房间
# @Software: PyCharmCE
from tools.my_methods import *


class Solution:
    def canVisitAllRooms(self, rooms: List[List[int]]) -> bool:
        ans = False
        if not rooms:
            return ans
        n = len(rooms)
        ss = [False for i in range(n)]

        def dfs(x):
            if ss[x]:
                return
            ss[x] = True
            for y in rooms[x]:
                if not ss[y]:
                    dfs(y)

        dfs(0)
        ans = len([i for i in range(n) if ss[i]]) == n
        return ans


questions = [
    ([[1], [2], [3], []], ),
    ([[1, 3], [3, 0, 1], [2], [0]], ),
]

work(questions, lambda q: Solution().canVisitAllRooms(q[0]))
